﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// PhotoAdd 的摘要说明
    /// </summary>
    public class PhotoAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            Photo_BLL bll = new Photo_BLL();
            try
            {
                context.Response.ContentType = "text/plain";

                for (int i = 0; i < 5; i++)
                {
                    string picurl = context.Request["picurl_" + i];
                    string imgName = context.Request["imgName_" + i];
                    string imgDes = context.Request["imgDes_" + i];
                    short imgHot = short.Parse(context.Request["imgHot_" + i]);
                    int imgCover = string.IsNullOrEmpty(context.Request["imgCover_" + i]) ? 0 : 1;

                    if (string.IsNullOrEmpty(imgName))
                        imgName = "未命名";

                    if (picurl.EndsWith("Images/cover_default.jpg"))
                    {
                        picurl = AppConst.Domain + "" + picurl;
                        continue;
                    };

                    PHOTO photo = new PHOTO()
                    {
                        PHOTO_ID = Util.GetUID(),
                        PHOTO_NAME = imgName,
                        PHOTO_URL = picurl,
                        PHOTO_COVER = (short)imgCover,
                        PHOTO_DES = imgDes,
                        PHOTO_HOT = imgHot
                    };

                    info = bll.Add(photo);

                    System.Threading.Thread.Sleep(123);
                }
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加图片失败");
                info.Success = false;
                info.Title = "错误";
                info.Tip = "<h5><p>添加图片失败</p></h5>";
                info.Url = "PhotoAdd.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}